package com.hemi.rhet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

import org.apache.http.ParseException;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.Toast;

import com.hemi.rhet.html.HtmlTemplate;
import com.hemi.rhet.html.TopicList;
import com.hemi.rhet.js.JavaScriptUtil;
import com.hemi.rhet.util.FileUtil;
import com.hemi.rhet.util.HttpUtil;

public class JavaScriptInterfaceBinder {

	public static JavaScriptInterfaceBinder INSTANCE = new JavaScriptInterfaceBinder();
	private JavaScriptInterfaceBinder(){}
	public void addCallCaremaAblity(Activity act,WebView mWebView, Handler hander ){
		mWebView.addJavascriptInterface(new JavaScriptInterfaceCallCarema(act,hander), "android_carema");   
	}
	
	public void addWebDataPostAblity(Activity act,WebView mWebView, Handler hander ){
		mWebView.addJavascriptInterface(new JavaScriptInterface4POSTDataHttpClient(act,mWebView,hander), "android_post");   
	}
	
	public void addWebDataUrlRedirectAblity(Activity act,WebView mWebView, Handler hander ){
		mWebView.addJavascriptInterface(new JavaScriptInterface2URL(act,mWebView,hander), "android_url");   
	}
	public void addWebImageSelectAblity(Activity act,WebView mWebView, Handler hander ){
		mWebView.addJavascriptInterface(new JavaScriptInterfaceImageSelect(act,mWebView,hander), "image_select");   
	}
	
	public void addShowTopicDetailAblity(Activity act,WebView mWebView, Handler hander ){
		mWebView.addJavascriptInterface(new JavaScriptInterface2TopicDetail(act,mWebView,hander), "android_topic_detail");   
	}
	 
	public void showSrcImage(Activity act ,String imageUrl){
		
	}
	
	 class JavaScriptInterface2TopicDetail {
	    	Handler mHandler = null;
	    	Activity act=null;
	    	WebView mWebView= null;
	    	public JavaScriptInterface2TopicDetail(Activity act,WebView mWebView,Handler hander){
	    		this.mHandler = hander;
	    		this.act = act;
	    		this.mWebView = mWebView;
	    	}
	        public void clickOnAndroid(final String url) {   
	      	  mHandler.post(new Runnable() {   
	                public void run() {   
	                	Intent i = new Intent(act, ActivityTopicDetail.class);
    					i.putExtra("detail_url",url);
    					act.startActivity(i);	
	                }});   
	        }   
	    }
	
    final class JavaScriptInterfaceImageSelect {   
    	Handler mHandler = null;
    	Activity act=null;
    	WebView mWebView2= null;
    	public JavaScriptInterfaceImageSelect(Activity act,WebView mWebView,Handler hander){
    		this.mHandler = hander;
    		this.act = act;
    		this.mWebView2 = mWebView;
    	}
        public void clickOnAndroid(final String imageId,final String event,final String fileIdItemId,final String showImageurl) { 
            mHandler.post(new Runnable() {   
                public void run() {
                	LayoutInflater inflater=act.getLayoutInflater();
            		View linearlayout= inflater.inflate(R.layout.image_preview, null);
            		Dialog	dialog  =	new AlertDialog.Builder(act).setView(linearlayout).setIcon(R.drawable.btn_insert_pics_res).create();
            		   dialog.show();    
            		WindowManager wm = (WindowManager) act.getSystemService(Context.WINDOW_SERVICE); 
            		Display display = wm.getDefaultDisplay(); 
            		final WebView  mWebView  = (WebView) linearlayout.findViewById(R.id.imagePreive_view); 
            		mWebView.setInitialScale(100);//初始显示比例100%
            		 WebSettings webSettings = mWebView.getSettings(); 
                    webSettings.setSavePassword(false);   
                    webSettings.setSaveFormData(false);   
                    webSettings.setJavaScriptEnabled(true);   
                    webSettings.setSupportZoom(true);   
                    webSettings.setBuiltInZoomControls(true);
                    webSettings.setUserAgentString("ff");
                    ImageView  timg  = (ImageView) linearlayout.findViewById(R.id.image_turn); 
                    timg.setOnClickListener(new OnClickListener(){
						public void onClick(View arg0) {
							// TODO Auto-generated method stub
							 mWebView.loadUrl("javascript:timage()");
						}
                    });
                    String idd = FileUtil.getUniqueId();
            		mWebView.setBackgroundColor(14737637);
            		String imageId = "_id"+idd;
            		HashMap params = new HashMap();
            		params.put("showImageurl",showImageurl);
            		params.put("imageId",imageId);
            		params.put("isIssured","true");
            		String html=TopicList.createNew().compile(act,"image_preview.template", null,params);
            		mWebView.loadDataWithBaseURL("file:///android_asset/.", html, "text/html", "utf-8", null);  
                }   
            });   
        }   
    }  
	
	
    final class JavaScriptInterfaceCallCarema {   
    	Handler mHandler = null;
    	Activity act=null;
    	public JavaScriptInterfaceCallCarema(Activity act,Handler hander){
    		this.mHandler = hander;
    		this.act = act;
    	}
        public void clickOnAndroid(final String imageContainerIdp,final String fileIdItemNamep) { 
            mHandler.post(new Runnable() {   
                public void run() {
                	Intent i= new Intent();
                	act.getIntent().putExtra("imageContainerId", imageContainerIdp);
                	act.getIntent().putExtra("fileIdItemName", fileIdItemNamep);
            		i.setClass(act, PhotoUpload.class);
            		act.startActivityForResult(i,Menu.FIRST);
                }   
            });   
        }   
    }   
    
    
    
   
    
    
    class JavaScriptInterface2URL {
    	Handler mHandler = null;
    	Activity act=null;
    	WebView mWebView= null;
    	public JavaScriptInterface2URL(Activity act,WebView mWebView,Handler hander){
    		this.mHandler = hander;
    		this.act = act;
    		this.mWebView = mWebView;
    	}
    	
        public void clickOnAndroid(final String url) {   
      	  mHandler.post(new Runnable() {   
                public void run() {   
                    String url2 = HttpUtil.buildUrl(url);
              	  mWebView.loadUrl(url2); 
                }});   
        }   
    }
    

class JavaScriptInterface4POSTDataHttpClient {
	
	Handler mHandler = null;
	Activity act=null;
	WebView mWebView= null;
	public JavaScriptInterface4POSTDataHttpClient(Activity act,WebView mWebView,Handler hander){
		this.mHandler = hander;
		this.act = act;
		this.mWebView = mWebView;
	}
  public void clickOnAndroid(final String url,final String content) {  
  	 mHandler.post(new Runnable() {   
           public void run() { 
      	 String urlStr =	HttpUtil.buildUrl(url);
    		 	 HashMap mapparams = new HashMap();
    		 	try {
    		 		JSONObject d = new JSONObject(content);
					Iterator it = d.keys();
					while(it.hasNext())
					{
						String key  =(String)it.next();
						mapparams.put(key, d.get(key));
					}
					HashMap rcontent = HttpUtil.postUrl4ResultMap(urlStr, mapparams);
					if(rcontent!=null && rcontent.get("code")!=null && rcontent.get("code").equals("1"))
					{
			             String url = HttpUtil.buildUrl(mapparams.get("_redirectUrl_")+"");
			             mWebView.loadUrl(url); 
					}else{
						Toast.makeText(act, "操作失败！",Toast.LENGTH_SHORT).show();   
					}
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (ParseException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} 
           }
  	 });  
  }


}

    
}
